package com.lqm.web.controller.home;

import com.lqm.entity.home.HomeUser;
import com.lqm.entity.home.UserAddress;
import com.lqm.service.home.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("home")
public class UserAddressController {
    @Autowired
    UserAddressService userAddressService;

    /**
     * 获取一个用户的所有地址
     * @param session
     * @return
     */
    @RequestMapping("getUserAllAddress")
    public Map<String ,Object> getUserAllAddress(HttpSession session){
        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        List<UserAddress> all = userAddressService.findAll(homeUser);
        System.out.println(all);
        map.put("data",all);
        map.put("code",0);
        return map;
    }

    @RequestMapping("setDefaultUserAddress")
    public Map<String ,Object> setDefaultUserAddress(Integer id,HttpSession session){
        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        UserAddress userAddress = new UserAddress();
        userAddress.setAddressId(id);
        userAddressService.setDefaultUserAddress(userAddress,homeUser);
        map.put("code",0);
        return map;
    }

    /**
     * 给用户添加一个地址
     * @param userAddress
     * @param session
     * @return
     */
    @RequestMapping("addUserAddress")
    public Map<String ,Object> addUserAddress(UserAddress userAddress,String addressProvide,HttpSession session){
        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        userAddress.setHomeUser(homeUser);
        userAddress.setAddressProvid(addressProvide);
/*        System.out.println(userAddress);
        System.out.println(addressProvide);*/
        UserAddress save = userAddressService.save(userAddress);
        if (save!=null){
            map.put("code",0);
            map.put("msg","添加成功");
        }else {
            map.put("code",1);
            map.put("msg","添加失败");
        }
        return map;
    }



    @RequestMapping("delAddress")
    public Map<String ,Object> delAddress(UserAddress userAddress,HttpSession session){
        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        userAddress.setHomeUser(homeUser);

/*        System.out.println(userAddress);
        System.out.println(addressProvide);*/
        userAddressService.delUserAddress(userAddress);

            map.put("code",0);
            map.put("msg","删除成功");

        return map;
    }


    @RequestMapping("updateAddress")
    public Map<String ,Object> updateAddress(UserAddress userAddress,HttpSession session){
        HashMap<String, Object> map = new HashMap<>(0);
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        userAddress.setHomeUser(homeUser);

        userAddressService.updateAddress(userAddress);

        map.put("code",0);
        map.put("msg","修改成功");

        return map;
    }

}
